package com.ruoyi.project.system.partymember.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.partymember.service.IPartyMemberService;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.service.IPostService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.partymember.domain.PartyMember;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 党员信息Controller
 * 
 * @author ruoyi
 * @date 2023-07-15
 */
@Controller
@RequestMapping("/system/partymember/member")
public class PartyMemberController extends BaseController
{
    private String prefix = "system/partymember/member";

    @Autowired
    private IPartyMemberService partyMemberService;
    
    @Autowired
    private IUserService userService;
    
    @Autowired
    private IDeptService deptService;
    
    @Autowired
    private IPostService postService;

    @RequiresPermissions("system:partymember:member:view")
    @GetMapping()
    public String member()
    {
        return prefix + "/member";
    }

    /**
     * 查询党员信息列表
     */
    @RequiresPermissions("system:partymember:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PartyMember partyMember)
    {
        startPage();
        List<PartyMember> list = partyMemberService.selectPartyMemberList(partyMember);
        return getDataTable(list);
    }

    /**
     * 导出党员信息列表
     */
    @RequiresPermissions("system:partymember:member:export")
    @Log(title = "党员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PartyMember partyMember)
    {
        List<PartyMember> list = partyMemberService.selectPartyMemberList(partyMember);
        ExcelUtil<PartyMember> util = new ExcelUtil<PartyMember>(PartyMember.class);
        return util.exportExcel(list, "党员信息数据");
    }



    /**
     * 新增党员信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        // 获取所有用户列表
        List<User> users = userService.selectUserList(new User());
        mmap.put("users", users);
        
        // 获取所有部门列表
        List<Dept> depts = deptService.selectDeptList(new Dept());
        mmap.put("depts", depts);
        
        return prefix + "/add";
    }

    /**
     * 选择党员页面
     */
    @GetMapping("/select")
    public String select()
    {
        return prefix + "/select";
    }

    /**
     * 新增保存党员信息
     */
    @RequiresPermissions("system:partymember:member:add")
    @Log(title = "党员信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody
                              PartyMember partyMember)
    {
        try {
            if (partyMember.getApplicationPath() == null && partyMember.getApplicationPath().isEmpty()) {
                // 上传入党申请书文件

                partyMember.setApplicationPath("");
            }
            partyMember.setCreateBy(getLoginName());
            return toAjax(partyMemberService.insertPartyMember(partyMember));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 查看党员信息明细
     */
    @GetMapping("/view/{id}")
    @Log(title = "党员信息", businessType = BusinessType.OTHER)
    public String view(@PathVariable("id") Long id, ModelMap mmap)
    {
        PartyMember partyMember = partyMemberService.selectPartyMemberById(id);
        mmap.put("partyMember", partyMember);
        return prefix + "/view";
    }

    /**
     * 修改党员信息
     */
    @RequiresPermissions("system:partymember:member:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PartyMember partyMember = partyMemberService.selectPartyMemberById(id);
        mmap.put("partyMember", partyMember);
        
        // 获取所有用户列表
        List<User> users = userService.selectUserList(new User());
        mmap.put("users", users);
        
        // 获取所有部门列表
        List<Dept> depts = deptService.selectDeptList(new Dept());
        mmap.put("depts", depts);
        
        // 获取所有岗位列表
        List<Post> posts = postService.selectPostAll();
        mmap.put("posts", posts);
        
        return prefix + "/edit";
    }

    /**
     * 修改保存党员信息
     */
    @RequiresPermissions("system:partymember:member:edit")
    @Log(title = "党员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody PartyMember partyMember)
    {
        try {
            if (partyMember.getApplicationPath() == null && partyMember.getApplicationPath().isEmpty()) {
                // 上传入党申请书文件
                partyMember.setApplicationPath("");
            }
            partyMember.setUpdateBy(getLoginName());
            return toAjax(partyMemberService.updatePartyMember(partyMember));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除党员信息
     */
    @RequiresPermissions("system:partymember:member:remove")
    @Log(title = "党员信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(partyMemberService.deletePartyMemberById(Long.valueOf(ids)));
    }
    
    /**
     * 加载部门列表树
     */
    @RequiresPermissions("system:partymember:member:list")
    @GetMapping("/deptTreeData")
    @ResponseBody
    public List<Ztree> deptTreeData()
    {
        List<Ztree> ztrees = deptService.selectDeptTree(new Dept());
        return ztrees;
    }
    
    /**
     * 校验党员编号唯一性
     */
    @PostMapping("/checkPartyNoUnique")
    @ResponseBody
    public boolean checkPartyNoUnique(PartyMember partyMember)
    {
        return partyMemberService.checkPartyNoUnique(partyMember);
    }
    
    /**
     * 文件上传接口
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file)
    {
        try {
            if (file.isEmpty()) {
                return error("文件为空");
            }
            // 上传文件
            String filePath = RuoYiConfig.getProfile();
            String fileName = FileUploadUtils.upload(filePath, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, true);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            return ajax;
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}